package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.customer.entity.CmLimitChangeLog;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerLimitInfoMapper;
import com.jrx.anytxn.customer.service.ICustomerLimitInfoService;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.param.utils.BizTypeProducts;
import com.jrx.anytxn.reporting.entity.AmlIATrading;
import com.jrx.anytxn.reporting.enums.RepaymentEnum;
import com.jrx.anytxn.reporting.job.xib.bean.AmlIATradingBean;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class MockTradingPaymentProcessor implements ItemProcessor<TlPaymentLog, AmlIATradingBean> {

    @Resource
    private ICustomerService customerService;

    @Resource
    private ExtCmCustomerLimitInfoMapper extCmCustomerLimitInfoMapper;

    @Resource
    ICustomerLimitInfoService iCustomerLimitInfoService;

    @Override
    public AmlIATradingBean process(TlPaymentLog tlPaymentLog) throws Exception {
        AmlIATradingBean amlIATradingBean = new AmlIATradingBean();
        List<AmlIATrading> amlIATradings = new ArrayList<>();

        //获取客户一级信息
        CmCustomerFirstLevel firstLevel = customerService.getFirstLevelByCondition(tlPaymentLog.getCustomerId(), tlPaymentLog.getOrganizationId(), tlPaymentLog.getTenantId());

        //获取客户二级信息
        CmCustomerSecondLevel secondLevel = customerService.getSecondByChannelAndCustomerIdAndTensnId(tlPaymentLog.getChannel(), tlPaymentLog.getCustomerId(), tlPaymentLog.getTenantId(), tlPaymentLog.getProductId());

        AmlIATrading amlIATrading = new AmlIATrading();
        //1 ETL_DT	数据日期
        amlIATrading.setEtlDt(tlPaymentLog.getCollateDate());
        //2 BUSINESS_ID	业务标示号 业务流水号
        amlIATrading.setBusinessId(tlPaymentLog.getSeqNo());
        //3 TRADE_DATE	交易日期
        amlIATrading.setTradeDate(tlPaymentLog.getCollateDate());
        //4 TRADE_TIME	交易时间
        amlIATrading.setTradeTime(tlPaymentLog.getLaunchTime());
        //5 SERIAL_NUMBER	流水号-与业务标志号相同
        String serialNumber = tlPaymentLog.getPayId();//流水号
        if(null == serialNumber){
            serialNumber = SeqUtils.getRandomNumber(32);
        }
        if(serialNumber.length() > 32){
            serialNumber = serialNumber.substring(serialNumber.length()-32);
        }

        amlIATrading.setSerialNumber(serialNumber);
        //6 VOUCHER_NO	传票号
        amlIATrading.setVoucherNo(tlPaymentLog.getPayId());
        //7 SIURCE_TRADE_ID	源交易代码-与业务标志号相同
        String siurceTradeId = tlPaymentLog.getRefNo();
        if(null != siurceTradeId && siurceTradeId.length() > 10){
            siurceTradeId = siurceTradeId.substring(siurceTradeId.length()-10);
        }
        amlIATrading.setSiurceTradeId(siurceTradeId);
        //8 SOURCE_SYSTEM_TYPE	来源系统-CCS
        amlIATrading.setSourceSystemType("CCS");
        //9 TRADE_ORGANIZATION_ID	交易机构-客户所属机构，总账机构
        //10 TRADE_ORGANIZATION_COUNTRY	交易机构归属国家
        amlIATrading.setTradeOrganizationCountry("CHN");
        //11 TRADE_ORGANIZATION_REGION	交易机构归属行政区划
        amlIATrading.setTradeOrganizationRegion(secondLevel.getBankOrgNo());
        //12 CUSTOMER_ID	客户号
        amlIATrading.setCustomerId(firstLevel.getEcifId());
        //13 CUSTOMER_TYPE	客户类型-个人客户
        amlIATrading.setCustomerType("P");
        //14 CUSTOMER_NAME	客户名称
        amlIATrading.setCustomerName(firstLevel.getChineseName());
        // 通过产品编号判断同系列有哪些产品编号
        List<String> seriesProducts = BizTypeProducts.getSeriesProductIds(tlPaymentLog.getProductId());
        //用户是否已经有对应的综合额度信息
        // 获取用户"首笔贷款额度信息"
        CmLimitChangeLog firstLimitInfo = iCustomerLimitInfoService.queryFirstLimitChangeLog(secondLevel.getTenantId(), secondLevel.getCustomerId(), seriesProducts);
        //15 ACCOUNT	账号-借据号
        amlIATrading.setAccount(firstLimitInfo.getEtlContractNumber());

        //16 TRADE_MODE	交易方式-000151
        amlIATrading.setTradeMode("000151");
        //17 RMB_FOREIGN_CURRENCY_TAG	本外币标识-本
        amlIATrading.setRmbForeignCurrencyTag("1");
        //18 CASH_TRANSFER_TAG	现转标识-转
        amlIATrading.setCashTransferTag("01");
        //20 CHANNEL	交易渠道-其他电子银行
        amlIATrading.setChannel("07");
        //21 RECEIPT_PAYMENT_TAG	收付标识-放款付，还款收
        amlIATrading.setReceiptPaymentTag("01");
        //22  COUNTRY_ID	交易发生国别-CHN？
        amlIATrading.setCountryId("CHN");
        //23 REGION	交易发生行政区划-厦门
        amlIATrading.setRegion(secondLevel.getBankOrgNo());
        //24 DIRECTION_COUNTRY	交易方向国别-CHN
        amlIATrading.setDirectionCountry("CHN");
        //25 DIRECTION_REGION	交易方向行政区划-对方银行卡所在区划；同金融机构。 TODO
        amlIATrading.setDirectionRegion(secondLevel.getBankOrgNo());
        //26 FINANCIAL_INSTITUTION_COUNTRY	对方金融机构国家-CHN
        amlIATrading.setFinancialInstitutionCountry("CHN");
        //27 FINANCIAL_INSTITUTION_REGION	对方金融机构行政区划代码-对方银行卡所在区划；财付通、支付宝等第三方支付公司或银行 TODO
        amlIATrading.setFinancialInstitutionRegion(secondLevel.getBankOrgNo());
        //28 ORIGIN_USAGE	资金来源或用途-放款（消费贷）；还款
        amlIATrading.setOriginUsage("还款");
        //29 CURRENCY	币种
        amlIATrading.setCurrency("CNY");
        //30 AMOUNT	原币种金额
        amlIATrading.setAmount(tlPaymentLog.getAmount());
        //31 AMOUNT_CNY	金额折人民币
        amlIATrading.setAmountCny(tlPaymentLog.getAmount());
        //32 AMOUNT_USD	金额折美-按当天即期汇率 TODO
        amlIATrading.setAmountUsd(BigDecimal.ZERO);
        //34 PAIRING_FINANCIAL_SETUP_NAME	对方金融机构名称 TODO
        amlIATrading.setPairingFinancialSetupName(secondLevel.getBankOrgNo());
        //35 PAIRING_FINANCIAL_SETUP_TYPE	对方金融机构代码类型 TODO
        amlIATrading.setPairingFinancialSetupType(secondLevel.getBankOrgNo());
        //36 PAIRING_FINANCIAL_SETUP_ID	对方金融机构网点代码 TODO
        amlIATrading.setPairingFinancialSetupId(secondLevel.getBankOrgNo());
        //37 PAIRING_CUSTOMER_ID 交易对手客户号-本行有，非本行空
        amlIATrading.setPairingCustomerId(firstLevel.getEcifId());
        //38 PAIRING_CUSTOMER_NAME	交易对手姓名-放款给商户信息，还款
        amlIATrading.setPairingCustomerName(RepaymentEnum.ACQUIRING.getCname());
        //39 PAIRING_CUSTOMER_TYPE	交易对手客户类型-个人或公司
        amlIATrading.setPairingCustomerType("P");
        //40 PAIRING_INNER_ACCOUNT_TAG	交易对手内部帐标识-可为空
        amlIATrading.setPairingInnerAccountTag(" ");
        //41 PAIRING_IS_CUSTOMER_TAG	交易对手是否我行客户
        amlIATrading.setPairingIsCustomerTag("0");
        //42 BANK_COMMERCIAL_TENANT_TAG	是否我行商户
        amlIATrading.setBankCommercialTenantTag("0");
        //43 COMMERCIAL_TENANT_CODE	商户编码
        amlIATrading.setCommercialTenantCode("");
        //47 PAIRING_ACCOUNT	交易对手账号-还款统一银行卡，放款120002；网联给了账户，如果没给就填网联清算账户。
        amlIATrading.setPairingAccount(tlPaymentLog.getPayerAccount());
        //48 PAIRING_ACCOUNT_TYPE	交易对手账户类型-还款以银行卡来还，填120017银行卡；放款先不填
        amlIATrading.setPairingAccountType("120017");
        //56 INTERNATIONAL_TRADE_TAG	是否跨境交易
        amlIATrading.setInternationalTradeTag("0");
        //60 IP	交易IP地址 TODO
        amlIATrading.setIp(" ");
        //61 MAC TODO
        amlIATrading.setMac(" ");
        //71 BUSINESS_TYPE	业务类型-仰章，仅包含消费、还款
        amlIATrading.setBusinessType("12");
        //72 BUSINESS_REMARKS	业务备注
        amlIATrading.setBusinessRemarks("还款");
        //75 NON_OTC_TYPE	非柜台交易方式-99
        amlIATrading.setNonOtcType("99");
        //76 NON_OTC_TYPE_DESCRIPTION	非柜台交易方式说明-兴安花
        amlIATrading.setNonOtcTypeDescription("兴安花");
        //77 NON_OTC_FACILITY_ID	非柜台交易介质的设备代码-交易手机号
        amlIATrading.setNonOtcFacilityId(secondLevel.getMobile());
        //78 BANK_PARMENTAUTHORITY_BTC 银行与支付机构之间的业务交易编码-网联等渠道的交易流水号 TODO 长度不够
        amlIATrading.setBankParmentauthorityBtc(" ");
        //79 CREDIT_CARD_TRADE_TAG	是否信用卡交易
        amlIATrading.setCreditCardTradeTag("0");
        //80 TRADE_TYPE	交易类型-其他，仰章代码值
        amlIATrading.setTradeType("03");
        //82 DESCRIPTION	摘要
        amlIATrading.setDescription(" ");
        //83 CALCULATION_TAG	是否计算
        amlIATrading.setCalculationTag("1");
        //90 IS_CASH_SETTLEMENT	是否现钞结汇(1-是，0-否)-否
        amlIATrading.setIsCashSettlement("0");
        //91 IS_CASH_SALE	是否现钞售汇(1-是，0-否)-否
        amlIATrading.setIsCashSale("0");
        //92 IS_CASH_EXCHANGE	是否外币现钞兑换(1-是，0-否)-否
        amlIATrading.setIsCashExchange("0");
        //93 IS_CASH_REMITTANCE	是否现金汇款(1-是，0-否)-否
        amlIATrading.setIsCashRemittance("0");
        //97 SETTLE_TYPE	业务类型，见详细表I_SETTLE_TYPE，同73
        amlIATrading.setSettleType("12");
        //98 SELF_DECL_FLAG	是否汇款申报(1-是，0-否)-否
        amlIATrading.setSelfDeclFlag("0");
        amlIATrading.setTenantId(tlPaymentLog.getTenantId());

        amlIATradings.add(amlIATrading);
        amlIATradingBean.setAmlIATradingList(amlIATradings);

        return amlIATradingBean;
    }

}
